import pygame
from pygame.locals import *
from costants import *
##########################################################
##########################################################
class Vect:
	def __init__(self,x,y):
		self.x=x
		self.y=y
		
	def __add__(self,v):
		x=self.x+v.x
		y=self.y+v.y
		return Vect(x,y)
	
	def __sub__(self,v):
		x=self.x-v.x
		y=self.y-v.y
		return Vect(x,y)
	
	def __rmul__(self,num):
		x=self.x*num
		y=self.y*num
		return Vect(x,y)
	
##########################################################
class Bar:
	def __init__(self,screen):
		self.pos=Vect((ARK_WIDTH+ARK_XOFFSET)*0.5,DEAD_LINE)
		self.screen=screen
		self.image=pygame.image.load(BAR_IMAGE).convert_alpha()
		self.width=self.image.get_width()
		self.rect=self.image.get_rect(midtop=(self.pos.x,self.pos.y))
		self.past_rect=self.rect
		
	def move_left(self,speed):
		speed=speed/10
		i=0
		while i<10:
			pos=self.pos+speed*Vect(-1,0)
			if self.pos.x>self.width*0.5+ARK_XOFFSET: self.pos=pos
			else: 
				self.pos=Vect(self.width*0.5+ARK_XOFFSET,self.pos.y)
				i=10
			i+=1
		self.past_rect=self.rect
		self.rect=self.image.get_rect(midtop=(self.pos.x,self.pos.y))
		
	def move_right(self,speed):
		speed=speed/10
		i=0
		while i<10:
			pos=self.pos+speed*Vect(1,0)
			if self.pos.x<=-self.width*0.5+ARK_RECT.right: self.pos=pos
			else: 
				self.pos=Vect(ARK_RECT.right-self.width*0.5,self.pos.y)
				i=10
			i+=1
		self.past_rect=self.rect
		self.rect=self.image.get_rect(midtop=(self.pos.x,self.pos.y))
		
	def blit(self):
		self.screen.blit(self.image,self.rect)
		
	def mid_point(self):
		return self.pos
##########################################################
class Ball:
	def __init__(self,x,y,screen):
		self.pos=Vect(x,y)
		self.screen=screen
		self.image=pygame.image.load(BALL_IMAGE).convert_alpha()
		self.rect=self.image.get_rect(center=(self.pos.x,self.pos.y))
		self.past_rect=self.rect
		self.diameter=self.image.get_width()
		self.state='alive'
		self.active=False
		self.dir=Vect(1,-1)
		
	def blit(self):
		self.screen.blit(self.image,self.rect)
		
	def move(self,speed,bar):
		pos=self.pos+speed*self.dir
		rect=self.image.get_rect(center=(pos.x,pos.y))
		if self.collide_with(bar,rect) == WALL: 
			self.wall_collision(rect)
			bar_hit=False
		if self.collide_with(bar,rect) == BAR: 
			self.bar_collision(rect)
			bar_hit=True
		if self.collide_with(bar,rect) == NOTHING:
			self.pos=pos
			self.past_rect=self.rect
			self.rect=rect
		if self.collide_with(bar,rect) == WALL_AND_BAR:
			self.wall_collision(rect)
			self.bar_collision(self.rect)
			bar_hit=True
		if not ARK_RECT.contains(self.rect) and not bar_hit: self.state='dead'
		
	def collide_with(self,bar,rect):
		if not ARK_LIFE_RECT.contains(rect) and not rect.colliderect(bar.rect): 
			return WALL
		if rect.colliderect(bar.rect): 
			return BAR
		if not ARK_LIFE_RECT.contains(rect) and rect.colliderect(bar.rect): 
			return WALL_AND_BAR
		else: return NOTHING
		
	def wall_collision(self,rect):
		self.past_rect=self.rect
		self.rect=rect
		if self.rect.left<=ARK_LIFE_RECT.left:
			self.pos=Vect(ARK_LIFE_RECT.left+self.diameter*0.5,\
			              self.rect.top+self.diameter*0.5)
			self.dir=Vect(-self.dir.x,self.dir.y)
			self.past_rect=self.rect
			self.rect=self.image.get_rect(center=(self.pos.x,self.pos.y))
		if self.rect.right>=ARK_LIFE_RECT.right:
			x=ARK_LIFE_RECT.right-self.diameter
			self.pos=Vect(x, self.rect.top+self.diameter*0.5)
			self.dir=Vect(-self.dir.x,self.dir.y)
			self.past_rect=self.rect
			self.rect=self.image.get_rect(center=(self.pos.x,self.pos.y))
		if self.rect.top<=ARK_LIFE_RECT.top:
			self.pos=Vect(self.rect.left+self.diameter*0.5,\
			              ARK_LIFE_RECT.top+self.diameter*0.5)
			self.dir=Vect(self.dir.x,-self.dir.y)
			self.past_rect=self.rect
			self.rect=self.image.get_rect(center=(self.pos.x,self.pos.y))
		if rect.bottom>=ARK_LIFE_RECT.bottom:
			self.pos=Vect(self.rect.center[0],self.rect.center[1])
			
	def bar_collision(self,rect):
		self.pos=Vect(rect.left+self.diameter*0.5,DEAD_LINE-self.diameter*0.5)
		self.dir=Vect(self.dir.x,-self.dir.y)
		self.past_rect=self.rect
		self.rect=self.image.get_rect(center=(self.pos.x,self.pos.y))
	def on_bar(self,bar):
		midtop_bar=bar.mid_point()
		self.pos = midtop_bar - 0.5*Vect(0,self.diameter)
		self.rect = self.image.get_rect(center=(self.pos.x,self.pos.y))
##########################################################
class Field:
	def __init__(self,screen):
		self.screen=screen
		self.image=pygame.image.load(FIELD_IMAGE).convert()
		
	def blit(self):
		self.screen.blit(self.image,ARK_RECT)
		#pygame.display.flip()
##########################################################
##########################################################
def blit_all(screen,sfondo,field,bar,ball,timer):
	screen.blit(sfondo,(0,0))
	screen.blit(sfondo,(0,0))
	field.blit()
	bar.blit()
	ball.blit()
	timer.blit(screen)
##########################################################
##########################################################